package com.antd.admin.server.controller;


import cn.hutool.core.util.StrUtil;
import com.antd.admin.api.entity.SysLog;
import com.antd.admin.server.service.ISysLogService;
import com.antd.common.core.util.R;
import com.antd.common.core.vo.PageVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2020-11-18
 */
@RestController
@RequestMapping("/log")
public class SysLogController {

    @Autowired
    private ISysLogService logService;
    @PostMapping
    public R<Object> saveLog(@RequestBody SysLog sysLog){
        return R.ok(logService.save(sysLog));
    }

    @GetMapping
    @ApiOperation("查询日志")
    public R<Object> list(PageVo pageVo,SysLog log){
        return R.ok(logService.page(new Page<>(pageVo.getPageNum(),pageVo.getPageSize()),
                Wrappers.<SysLog>query().lambda()
                .eq(StrUtil.isNotBlank(log.getRequestMethod()),SysLog::getRequestMethod,log.getRequestMethod())
                ));
    }
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('sys:log:delete')")
    @ApiOperation("删除日志")
    public R<Object> delete(String ids){
        List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        return R.ok(logService.removeByIds(idList));
    }
}
